package com.stu.wgt.test.service.Imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.stu.wgt.test.bean.Order;
import com.stu.wgt.test.bean.Product;
import com.stu.wgt.test.mapper.OrderMapper;
import com.stu.wgt.test.mapper.ProductMapper;
import com.stu.wgt.test.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wgt
 * @since 2024-01-20
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    OrderMapper orderMapper;
    @Autowired
    ProductMapper productMapper;

    @Override
    public int insert(Order order) {
        return orderMapper.insert(order);
    }

    @Override
    public int insertOrderList(List<Order> list) {
        return orderMapper.insertOrderList(list);
    }


    @Override
    public int delete(int id) {
        return orderMapper.deleteById(id);
    }

    @Override
    public int update(Order order) {
        QueryWrapper<Order> qw = new QueryWrapper<>();
        qw.eq("id", order.getId());
        return orderMapper.update(order, qw);
    }


    @Override
    // 更新库存
    public int updateNumber(int productId, int quantity) {
        if (!checkNumber(productId, quantity)) {
            return 0; // 一旦发现库存不足的商品，返回false
        }
        // 所有商品库存均足够
        Product product = productMapper.selectById(productId);
        int ans = product.getNumber() - quantity;
        if (ans < 0) {
            return 0;
        }
        product.setNumber(ans);
        QueryWrapper<Product> qw = new QueryWrapper<>();
        qw.eq("id", productId);
        return productMapper.update(product, qw);
    }

    @Override
    // 查看库存是否充足
    public boolean checkNumber(int productId, int quantity) {
        Product product = productMapper.selectById(productId);
        return product != null && product.getNumber() >= quantity;
    }

    @Override
    public List<Order> selectById(int id) {
        QueryWrapper<Order> qw = new QueryWrapper<>();
        qw.eq("id", id);
        return orderMapper.selectList(qw);
    }

    @Override
    public List<Order> selectByUserId(Integer id) {
        QueryWrapper<Order> qw = new QueryWrapper<>();
        qw.eq("user_id", id);
        return orderMapper.selectList(qw);
    }

    @Override
    public List<Order> selectByStatus(String status) {
        QueryWrapper<Order> qw = new QueryWrapper<>();
        qw.eq("status", status);
        return orderMapper.selectList(qw);
    }

    @Override
    public List<Order> selectByProductId(Integer productId) {
        QueryWrapper<Order> qw = new QueryWrapper<>();
        qw.eq("product_id", productId);
        return orderMapper.selectList(qw);
    }

    @Override
    public List<Order> selectAll() {
        return orderMapper.selectList(null);
    }

    @Override
    public List<Order> selectByUserIdAndStatus(Integer userId, String status) {
        QueryWrapper<Order> qw = new QueryWrapper<>();
        qw.eq("user_id", userId).eq("status", status);
        return orderMapper.selectList(qw);
    }

    @Override
    public IPage<Order> search(Integer userId, Integer merchantId, String status, int pageNum, int pageSize) {
        Page<Order> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Order> qw = new QueryWrapper<>();
        // 只有当 userId 不为空时，才添加该筛选条件
        if (userId != null) {
            qw.eq("user_id", userId);
        }

        // 只有当 productId 不为空时，才添加该筛选条件
        if (merchantId != null) {
            qw.eq("merchant_id", merchantId);
        }

        // 只有当 status 不为空时，才添加该筛选条件
        if (status != null && !status.isEmpty()) {
            qw.eq("status", status);
        }
        IPage<Order> IPage = orderMapper.selectPage(page, qw);
        return IPage;
    }

}
